package com.kc.gb.device.plugins.strip.driver;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AtopControlReceiveData implements Runnable {
    public AtopControl atopItem = null;
    public boolean IsClose = false;
    List<Integer> datas = new ArrayList<Integer>();
    InputStream is = null;
    int data_char = -1;

    public void run() {
        while (!IsClose) {
            try {
                if (atopItem != null && atopItem.IsConnect && atopItem.socket != null) {
                    is = atopItem.socket.getInputStream();
                    if (is.available() == 0) {
                        while (datas.size() >= 7) {
                            log.info("current read message end：SubCommand：" + datas.get(6) + "，addr：" + datas.get(7));
                            TagReturnInfo p_item = new TagReturnInfo();
                            p_item.Port = 2;
                            int message_length = datas.get(0);//消息长度
                            if (datas.get(2) == 96) {
                                p_item.Port = 1;
                            }
                            p_item.SubCommand = datas.get(6);
                            p_item.Abbr = datas.get(7);
                            //拍灯返回数据
                            if (p_item.SubCommand == 6 || p_item.SubCommand == 7) {
                                String return_str = "";

                                for (int i = 8; i < message_length; i++) {
                                    if (message_length == (i + 1)) {
                                        //最后一位是小数位,一般情况下不需要
                                        if (((int) datas.get(i)) > 0) {
                                            char a = (char) ((int) datas.get(i));
                                            return_str = return_str + a;
                                        }
                                    } else {
                                        char a = (char) ((int) datas.get(i));
                                        return_str = return_str + a;
                                    }

                                }
                                p_item.Data = return_str;
                                log.info("return data:" + return_str + ";length：" + return_str.length());
                            }
                            //标签自检
                            if (p_item.SubCommand == 9) {
                                int[] ccb_data = new int[250];
                                for (int k = 8; k < 258 && k < datas.size(); k++) {
                                    ccb_data[k - 8] = datas.get(k);
                                }
                                p_item.Data = atopItem.DiagResult(ccb_data);
                                atopItem.StatusMsg = p_item.Data;
                            }
                            //没有亮灯情况下按键
                            if (p_item.SubCommand == 100) {
                                p_item.MsgType = datas.get(8);
                                p_item.KeyType = datas.get(9);
                            }
                            log.info("SubCommand：" + p_item.SubCommand + "，abbr：" + p_item.Abbr + ",MsgType:" + p_item.MsgType + ",KeyType：" + p_item.KeyType + ",Data:" + p_item.Data);
                            if (datas.size() > message_length) {
                                log.info("continue process next message");
                                datas = datas.subList((int) (message_length), datas.size());
                            } else {
                                log.info("current message end");
                                datas.clear();
                            }
                        }


                    }

                    data_char = is.read();
                    datas.add(data_char);
                }

            } catch (IOException e) {
                log.error("read data error", e);
            }
        }
    }

}
